""" 
sw-520D 倾斜角传感器
接线:
    tilt 输入in --> GPIO26
    VCC --> 5V
    GND --> GND
"""

from machine import Pin
import time

TiltPin = 26  # 倾斜传感器Pin端口
LedPin = 15  # LED端口


def read_pin(led_pin, tilt_pin):
    """定时读取输入硬件状态"""
    while True:
        value = tilt_pin.value()
        if value == 0:
            led_pin.value(1)
            print("倾斜")
        else:
            led_pin.value(0)
            print("未倾斜, 平整")
        time.sleep(1)


# 程序入口
if __name__ == "__main__":
    led_pin = Pin(LedPin, Pin.OUT)  # 倾斜状态指示灯
    tilt_pin = Pin(TiltPin, Pin.IN, Pin.PULL_UP)  # 倾斜传感器输入端口
    read_pin(led_pin, tilt_pin)
